const MY_GAME_OBJECT = [];

export class MyGameObject{
    constructor(){
        MY_GAME_OBJECT.push(this);

        this.timedelta = 0;//时间间隔（上一帧与此帧的间隔）
        this.has_called_start = false;
        this.last_timestap = 0;//上一次执行的时刻
    }

    start() { //first 创建时执行一次

    }

    update() { //每一帧执行一次，除了第一次

    }

    on_destroy(){//删除之前执行
        
    }

    destroy() { //删除当前
        this.on_destroy();

        for(let i in MY_GAME_OBJECT) {
            const obj = MY_GAME_OBJECT[i];
            if(obj === this){ //循环查找当前值在数组中的什么位置然后删除
                MY_GAME_OBJECT.splice(i,1);
                return;
            }
        }
        for(let i in MY_GAME_OBJECT) {
            const obj = MY_GAME_OBJECT[i];
            if(obj.r == this.r && obj.c == this.c){ //循环查找当前值在数组中的什么位置然后删除
                MY_GAME_OBJECT.splice(i,1);
                return;
            }
        }
    }

}


const step = timestap => {
    for(let obj of MY_GAME_OBJECT) { //执行当前的start或者update
        if(!obj.has_called_start) {
            obj.has_called_start = true;
            obj.start();
            obj.last_timestap = timestap;
        }else{
            if(timestap - obj.last_timestap > 6)
            {
                obj.timedelta = timestap - obj.last_timestap;
                obj.update();
                obj.last_timestap = timestap;
            }
            
        }
    }
    
    requestAnimationFrame(step);
}

requestAnimationFrame(step)